package com.duoduo.mcc.thread;

import java.util.Random;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.duoduo.mcc.bean.MccMessage;
import com.duoduo.mcc.service.MccReceiveService;

/**
 * 消息生产线程
 * @author cgs1999@126.com
 * @date 2018年11月1日下午6:06:20
 */
public class MccCreateThread implements Runnable {
	private static String[] types = {"TypeA","TypeB","TypeC"};
	private Logger logger = LoggerFactory.getLogger(getClass());
	private MccReceiveService mccReceiveService;
	private String e164;
	private Random random;

	public MccCreateThread(MccReceiveService mccReceiveService, String e164) {
		this.mccReceiveService = mccReceiveService;
		this.e164 = e164;

		random = new Random(System.currentTimeMillis());
	}

	public void run() {
		while(true) {
			for(int i=0; i<10; i++) {
				String type = getMessageType();
				MccMessage message = new MccMessage(e164, type);
				message.setOperaData(String.format("data.%s.%s.%s", e164, type, generateUuid()));
				mccReceiveService.addMessage(message);
				
				logger.debug("会议 {} 产生数据 {}", e164, message.toString());
			}
			
			doSomething();
		}
	}
	
	private String getMessageType() {
		int rnd = 100 + random.nextInt(100);
		return types[rnd % types.length];
	}
	
	private String generateUuid() {
		return UUID.randomUUID().toString();
	}

	private void doSomething() {
		long sleep = 100 + random.nextInt(500);
		try {
			Thread.sleep(sleep);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
